package com.sinosoft.perfect.common.bean;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.core.Ordered;

public class GenericLoggerBean implements Ordered{
	
	private static Logger logger = LogManager.getLogger(GenericLoggerBean.class);
	
	public Object invoke(ProceedingJoinPoint pjp)throws Throwable{
		logger.info("Beginning method : " +  pjp.getTarget().getClass() + "." + pjp.getSignature().getName()+ "()");
		Object object = null;
		
		try {
			if(pjp instanceof MethodInvocationProceedingJoinPoint){
				object = pjp.proceed();
			}
		} catch (Throwable e) {
			logger.info(pjp.getTarget().getClass() + "." + pjp.getSignature().getName() + "() invoke error");
			logger.error("error info ["+e.getMessage()+"]");
			throw new Exception(e);
		} finally {
			logger.info("Ending method : " + pjp.getTarget().getClass() + "." + pjp.getSignature().getName() + "()");   
		}
		
		return object;
	}
	
	private int order = 1;

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}
	
}
